Skip to main content
ICT
Lesson A13 - Exceptions and File I/O
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

F. Writing to a File page 8 of 12

  1. Creating your own files is a little bit trickier than reading from them. There are two basic ways to write data to files: raw bytes and character streams. Raw byte writing is useful for items such as pictures. Character streams are used for writing plain text. This curriculum will focus only on character streams.

  1. This curriculum uses the java.io.FileWriter class. It has two basic constructors:

    FileWriter file = new FileWriter(“test.txt”);
    FileWriter file2 = new FileWriter(“test2.txt”, true);

    The first constructor opens a basic FileWriter object that points at the file “test.txt” in the same directory where the Java files are being run. When this file is first opened and written to, all of the data that was previously stored in the file will be erased. The second constructor indicates that the new data being sent to the file will be appended to the end of the file. In either case, if the file does not exist, then Java will attempt to create a new file with the indicated name and at the indicated location.

  2. Writing data to the file is done by using the FileWriter.write(String, int, int) method. The String is the data that will be written to the file. The first int is where to start writing the data in the String. The second int indicates how many characters of the String to actually write. For example:

    String one = “#Hello!!!”;
    FileWriter out = new FileWriter(“test.txt”);
    out.write(one, 1, 5);

    This will write only “Hello” to the file “test.txt.”

  3. Merely opening a file and writing to it is not enough to store your data in most cases. You know from personal experience that if you don’t save your work in a word processor, your work will not be there the next time you start up your computer. Data must be saved. This is done with FileWriter by calling the close() method when you are done writing data. This “closes” the output stream to the file and saves your data.

    Out.write(one, 1, 5);
    Out.close();

  4. What if there is some error in opening the file? That’s right - an exception is thrown and it must be dealt with just like in the Scanner class.

    String one = “Hello World!!!”;
    FileWriter out;
    try{
          out = new FileWriter(“test.txt”);
          out.write(one, 0, one.length());
          out.close();
    }catch(IOException i){
          System.out.println(“Error: “ + i.getMessage());
    }

  5. There is no equivalent to println() with the FileWriter class, so any newlines that you wish to create must be done with the ‘\n’ character.

  6. FileWriter only deals with writing Strings to the text files, which creates a little bit of a problem with writing numeric data. However, we can use the shortcut learned earlier in Lesson A10, Strings to change our other data types to Strings.

    String temp;
    int a = 5;
    temp = “” + a + “\n”;
    out.write(temp, 0, temp.length());
    double p = 3.14;
    temp = “” + p + “\n”;
    out.write(temp, 0, temp.length());
    boolean test = true;
    temp = “” + test + “\n”;
    out.write(temp, 0, temp.length());

  7. Because FileWriter requires you to specify how many characters of the given String to print out, you must be careful with the values that you give it. If the int value that you send is bigger than the String itself, you will get a StringIndexOutOfBoundsException when the FileWriter object tries to access characters in the String which do not exist. An easy way to prevent this from ever occurring is to always create a String object before the write method is called with the data you wish to output, place that String in the call to write, and use that String’s length() method for how many characters to print.

    String one = “Hello World!!!\n”;
    Out.write(one, 0, one.length());

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.